package com.example.jaden.washeradministrator;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;

import java.io.IOException;
import java.lang.reflect.Type;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import admin.Admin;
import dormitory.Dormitory;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import order.Order;
import washer.Washer;

import static activity.LoginPage.JSON;
import static android.content.ContentValues.TAG;

public class TransferData {

    /**
     * 登录验证
     * 用于在登录页面时发送用户名和密码 在服务器端判断
     * 若验证成功，则返回该用户的个人信息
     * @param urlPath
     * @param messageJson
     */
    public static void LoginAuthentication(String urlPath,String messageJson) throws JSONException, MalformedURLException {


                    URL url=new URL(urlPath);
                    OkHttpClient client=new OkHttpClient();
                    RequestBody requestBody=RequestBody.create(JSON,messageJson);
                    Request request=new Request.Builder()
                            .url(url)
                            .post(requestBody)
                            .build();
                    client.newCall(request).enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {

                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            /**
                             * 验证返回的数据是否合法
                             * 并且设置跟人信息
                             */
                            Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
                             String citizenInfo=response.body().string();
                            System.out.println(citizenInfo);
                            Type type=new TypeToken<ResponseMessage<Admin>>(){}.getType();
                            ResponseMessage<Admin> responseMessage =gson.fromJson(citizenInfo,type);

                            MyApplication application=MyApplication.getInstance();
                            application.setResponseMessage(responseMessage);
                            application.setLoginOk(true);
                        }
                    });

    }

    /**
     * 注册
     * @param urlPath
     * @param messageJson
     * @throws JSONException
     * @throws MalformedURLException
     */
    public static void Register(String urlPath,String messageJson) throws JSONException, MalformedURLException {


        URL url=new URL(urlPath);
        OkHttpClient client=new OkHttpClient();
        RequestBody requestBody=RequestBody.create(JSON,messageJson);
        Request request=new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                /**
                 * 验证返回的数据是否合法
                 * 并且设置跟人信息
                 */
                Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
                String citizenInfo=response.body().string();
                Log.d(TAG, "onResponse: "+citizenInfo);
                //System.out.println(citizenInfo);
                //String citizenInfo=citizenInfo1;
                //System.out.println(citizenInfo.replace("\"","'"));
                //Type type=new TypeToken<ResponseMessage<Citizen>>(){}.getType();

                //将注册后服务器返回的居民信息解析
                ResponseMessage<Admin> responseMessage =gson.fromJson(citizenInfo,ResponseMessage.class);
                MyApplication application=MyApplication.getInstance();
                application.setResponseMessage(responseMessage);
                application.setRegisterOk(true);

            }
        });

    }
    /**
     * 获取用户信息
     * @param urlPath
     * @param messageJson
     * @throws JSONException
     * @throws MalformedURLException
     */
    public static void GetCitizenInfo(String urlPath,String messageJson) throws JSONException, MalformedURLException {


        URL url=new URL(urlPath);
        OkHttpClient client=new OkHttpClient();
        RequestBody requestBody=RequestBody.create(JSON,messageJson);
        Request request=new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                /**
                 * 验证返回的数据是否合法
                 * 并且设置跟人信息
                 */
                Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
                String citizenInfo=response.body().string();
                System.out.println(citizenInfo);
                Type type=new TypeToken<ResponseMessage<Admin>>(){}.getType();
                //将注册后服务器返回的居民信息解析
                ResponseMessage<Admin> responseMessage =gson.fromJson(citizenInfo,type);

                MyApplication application=MyApplication.getInstance();
                application.setResponseMessage(responseMessage);
                application.setGetCitizenInfoOk(true);

            }
        });

    }
    /**
     * 存储个人信息
     * @param urlPath
     * @param messageJson
     * @throws JSONException
     * @throws MalformedURLException
     */
    public static void SavePersonalInformation(String urlPath,String messageJson) throws JSONException, MalformedURLException {

        URL url=new URL(urlPath);
        OkHttpClient client=new OkHttpClient();
        RequestBody requestBody=RequestBody.create(JSON,messageJson);
        Request request=new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                /**
                 * 验证返回的数据是否合法
                 * 并且设置跟人信息
                 */
                Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
                String citizenInfo=response.body().string();
                System.out.println(citizenInfo);

                //将注册后服务器返回的居民信息解析
                ResponseMessage responseMessage =gson.fromJson(citizenInfo,ResponseMessage.class);
                MyApplication application=MyApplication.getInstance();
                application.setResponseMessage(responseMessage);
                application.setSavePersonalInfoOK(true);

            }
        });

    }

    /**
     * 增加宿舍
     * @param urlPath
     * @param messageJson
     * @throws JSONException
     * @throws MalformedURLException
     */
    public static void AddDorm(String urlPath, String messageJson) throws JSONException, MalformedURLException {


        URL url=new URL(urlPath);
        OkHttpClient client=new OkHttpClient();
        RequestBody requestBody=RequestBody.create(JSON,messageJson);
        Request request=new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                /**
                 * 验证返回的数据是否合法
                 * 并且设置跟人信息
                 */
                Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
                String orderJson=response.body().string();
                System.out.println(orderJson);
                //String citizenInfo=citizenInfo1;
               // Type type=new TypeToken<ResponseMessage<Order>>(){}.getType();
                ResponseMessage<Dormitory> responseMessage =gson.fromJson(orderJson,ResponseMessage.class);
                //Order order=  responseMessage.getData();
                //List<Commodity.PurchasedCommodities>purchasedCommodities=order.getPurchasedCommodities();

                MyApplication application=MyApplication.getInstance();
                //将刚获取的订单加到订单列表中
                //List<Order>orderList=application.getOrderList();
                //orderList.add(order);
                //application.setOrderList(orderList);

                application.setResponseMessage(responseMessage);
                application.setAddDormOK(true);

            }
        });

    }

    /**
     * 取消订单
     * @param urlPath
     * @param messageJson
     * @throws JSONException
     * @throws MalformedURLException
     */
    public static void AddWasher(String urlPath, String messageJson) throws JSONException, MalformedURLException {


        URL url=new URL(urlPath);
        OkHttpClient client=new OkHttpClient();
        RequestBody requestBody=RequestBody.create(JSON,messageJson);
        Request request=new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
                String orderJson=response.body().string();
                //System.out.println(citizenInfo);
                //String citizenInfo=citizenInfo1;
                //Type type=new TypeToken<ResponseMessage<ArrayList<Order>>>(){}.getType();
                ResponseMessage responseMessage =gson.fromJson(orderJson,ResponseMessage.class);

                MyApplication application=MyApplication.getInstance();
                application.setResponseMessage(responseMessage);
                application.setAddWasherOK(true);

            }
        });

    }
    /**
     * 获取宿舍信息
     * @param urlPath
     * @param messageJson
     * @throws JSONException
     * @throws MalformedURLException
     */
    public static void GetDormList(String urlPath, String messageJson) throws JSONException, MalformedURLException {


        URL url=new URL(urlPath);
        OkHttpClient client=new OkHttpClient();
        RequestBody requestBody=RequestBody.create(JSON,messageJson);
        Request request=new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
                String dormJson=response.body().string();
                System.out.println(dormJson);
                //String citizenInfo=citizenInfo1;
                Type type=new TypeToken<ResponseMessage<ArrayList<Dormitory>>>(){}.getType();
                ResponseMessage<ArrayList<Order>> responseMessage =gson.fromJson(dormJson,type);

                MyApplication application=MyApplication.getInstance();
                application.setResponseMessage(responseMessage);
                application.setGetDormOK(true);
            }
        });

    }
    /**
     * 确认订单
     * @param urlPath
     * @param messageJson
     * @throws JSONException
     * @throws MalformedURLException
     */
    public static void ConfirmOrder(String urlPath,String messageJson) throws JSONException, MalformedURLException {


        URL url=new URL(urlPath);
        OkHttpClient client=new OkHttpClient();
        RequestBody requestBody=RequestBody.create(JSON,messageJson);
        Request request=new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
                String orderJson=response.body().string();
                //System.out.println(citizenInfo);
                //String citizenInfo=citizenInfo1;
                //Type type=new TypeToken<ResponseMessage<ArrayList<Order>>>(){}.getType();
                ResponseMessage<Order> responseMessage =gson.fromJson(orderJson,ResponseMessage.class);

                MyApplication application=MyApplication.getInstance();
                application.setResponseMessage(responseMessage);
                application.setConfirmOK(true);
            }
        });

    }
    /**
     * 选择订单
     * @param urlPath
     * @param messageJson
     * @throws JSONException
     * @throws MalformedURLException
     */
    public static void SelectOrder(String urlPath,String messageJson) throws JSONException, MalformedURLException {


        URL url=new URL(urlPath);
        OkHttpClient client=new OkHttpClient();
        RequestBody requestBody=RequestBody.create(JSON,messageJson);
        Request request=new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
                String orderJson=response.body().string();
                System.out.println(orderJson);
                Type type=new TypeToken<ResponseMessage<ArrayList<Order>>>(){}.getType();
                ResponseMessage<ArrayList<Order>> responseMessage =gson.fromJson(orderJson,type);
                List<Order> orderList=  responseMessage.getData();

                MyApplication application=MyApplication.getInstance();
                application.setOrderList(orderList);
                application.setResponseMessage(responseMessage);
                application.setSelectOrderOK(true);
            }
        });
    }
    /**
     * 获取某一宿舍楼的洗衣机信息
     * @param urlPath
     * @param messageJson
     * @throws JSONException
     * @throws MalformedURLException
     */
    public static void GetWasherList(String urlPath, String messageJson) throws JSONException, MalformedURLException {


        URL url=new URL(urlPath);
        OkHttpClient client=new OkHttpClient();
        RequestBody requestBody=RequestBody.create(JSON,messageJson);
        Request request=new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
                String orderJson=response.body().string();
                Type type=new TypeToken<ResponseMessage<ArrayList<Washer>>>(){}.getType();
                ResponseMessage<ArrayList<Washer>> responseMessage =gson.fromJson(orderJson,type);

                MyApplication application=MyApplication.getInstance();
                application.setResponseMessage(responseMessage);
                application.setGetWasherOk(true);
            }
        });

    }
}
